import java.util.HashMap;
import java.util.Map;

public class Solution {
    public int totalFruit(int[] f) {
        Map<Integer,Integer> hash = new HashMap<Integer,Integer>();
        int ret = 0;
        for(int left = 0,right = 0;right < f.length;right++) {
            int in = f[right];
            hash.put(in,hash.getOrDefault(in,0)+1);
            while(hash.size() > 2){
                int  out = f[left];
                hash.put(out,hash.get(out)-1);
                if(hash.get(out) == 0)
                    hash.remove(out);
                left++;
            }
            ret = Math.max(ret,right - left + 1);
        }
        return ret;
    }
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] f ={3,3,3,1,2,1,1,2,3,3,4};
        System.out.println(solution.totalFruit(f));
    }
}
